<?php
/**
 * bank_information model
 * @ 11/03/2013
 * @author Nguyen Van Nhieu <nhieunv@evolable.asia>
 */

class BankInformation extends AppModel{
	var $name = 'BankInformation';
	
	var $validate = array(
		'bank_name' => array(
			'minlength' => array(
				'rule' => 'notEmpty',
				'required' => true,
				'message' => Messages::bank_name_can_not_be_left_blank
			),
					
		),
		'branch_name' => array(
			'minlength' => array(
				'rule' => 'notEmpty',
				'required' => true,
				'message' => Messages::branch_name_can_not_be_left_blank
			)
		),
		'account_num' => array(
			'number' => array(
				'rule' => 'numeric',
				'message' => Messages::the_account_number_you_supplied_was_invalid_not_space
			),
			'maxLength' => array(
				'rule' => array('maxLength', 7),
				'message' => Messages::bank_account_number
			)
		),
		'payee_name' => array(
			'minlength' => array(
				'rule' => 'notEmpty',
				'required' => true,
				'message' => Messages::payee_name_can_not_be_left_blank
			)
		),
	);
	
	/**
	 * override this function, delete data
	 * @param int $id
	 * @return type
	 */
	public function deleteDataById($id) {
		return $this->delete($id);
	}
	
	/**
	 * 
	 * @param type $key
	 * @param type $value
	 * @return type
	 */
	public function deleteDataByKey($key, $value) {
		$conditions = array(
			$this->alias . '.' . $key => $value
		);
		return $this->deleteAll($conditions);
	}
	
	public function getBankInfoCsvExport() {
		$options['fields'] = array(
			$this->alias . ".id",
			"User.id",
			"Profile.first_name",
			"Profile.last_name",
			$this->alias . ".bank_name",
			$this->alias . ".branch_name",
			$this->alias . ".type",
			$this->alias . ".account_num",
			$this->alias . ".payee_name",
			"User.created",
			"User.role",
			"User.del_flg"
			);
		
		$options['joins'] = array(
			array(
				'table' => 'users',
				'alias' => 'User',
				'type' => 'left',
				'conditions' => array(
					$this->alias . '.user_id = User.id',
					'User.del_flg = 0'
				),
			),
			array(
				'table' => 'profiles',
				'alias' => 'Profile',
				'type' => 'left',
				'conditions' => array(
					'User.id = Profile.user_id'
				)
			)
		);
		
		//$options['recursive'] = -1;
		$bankInfo = $this->find('all', $options);
		$data = array();
		foreach ($bankInfo as $bank) {
			$data[] = array(
				$bank[$this->alias]['id'],
				$bank['User']['id'],
				$bank['Profile']['first_name'],
				$bank['Profile']['last_name'],
				$bank[$this->alias]['bank_name'],
				$bank[$this->alias]['branch_name'],
				$bank[$this->alias]['type'],
				$bank[$this->alias]['account_num'],
				$bank[$this->alias]['payee_name'],
				$bank['User']['created'],
				$bank['User']['role'],
				$bank['User']['del_flg'],
			);
		}
		return $data;
	}
	
}
?>
